﻿/**
* @private
* @license	GNU GPLv2
*
* Copyright (C) 2007  Polly Jex.
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
* MA  02110-1301, USA.
*
* @see	http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

package com.kirika.data.types {	

	/**
	 * @import
	 */
	
	import com.kirika.core.CoreStatic;
	
	/**
	 * Generador de UID (Identificadores Unicos)
	 * @author	Polly Jex (pollyjex@emc2zen.com.ar)
	 */
	public class UID extends CoreStatic {
		
		/**
		 * Template de creacion.
		 */
		private static const UID_TEMPLATE:Array = [ 8, 4, 4, 4, 12 ];
		
		/**
		 * Codigo del separador "-".
		 */
		private static const DASH_CHAR_CODE:uint = 45;
		
		/**
		 * Codigo de los caracteres "0123456789ABCDEF".
		 */
		private static const ALPHA_CHAR_CODES:Array = [ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 ];
		
		/**
		 * Crea y retorna un UID "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".
		 * @return	String
		 */
		public static function create():String {
			
			var a:uint;
			var b:uint;
			
			var TMP_INDEX:int = 0;
			var TMP_UID:Array = new Array( 36 );
			
			for( a = 0; a < UID_TEMPLATE.length; a++ ){
				
				for( b = 0; b < UID_TEMPLATE[ a ]; b++ ){
					TMP_UID[ TMP_INDEX++ ] = ALPHA_CHAR_CODES[ Math.floor( Math.random() *  16 ) ];
				}
				
				if( a < UID_TEMPLATE.length - 1 ){
					TMP_UID[ TMP_INDEX++ ] = DASH_CHAR_CODE;
				}
				
			}
			
			return String.fromCharCode.apply( null, TMP_UID );
			
		}
		
	}
	
}
